$(document).ready(function() {

			$("#tabs").tabs();

			var login = "";
			var password = "";			
			
			$("#rss").focus(function() {
				$("#errorId").text("");
			});
			
			$.ajax({
				type : "GET",
				url : "/rss/ajax/authentification",
				success : function(response) {
					login = response[0];
					password = response[1];
					// alert(login + ":" + password);
					restoreFeeds(login);
				},
				error : function(e) {
					alert("Error: " + e);
				}
			});

			/*
			 * Restores feeds
			*/
			function restoreFeeds(login) {
				$.ajax({
					type : "GET",
					url : "/rss/ajax/restoreFeeds",
					data : "username=" + login,
					success : function(feedsData) {
						//alert("restoreFeeds: " + feedsData);
						restore(feedsData);
					},
					error : function(e) {
						alert("Error: " + e);
					}
				});
			}

			function restore(feedsData) {
				var line = [];
				$.each(feedsData, function(key, value) {
					//alert("restore, key: " + key + ":" + " value: " + value);
					$.each(value, function(k, v) {
						//alert("k: " + k + ":" + " v: " + v);
						line.push(k);
						line.push(v);
						// alert("line: " + line);
						buildFeed(line);
						buildFeedEdit(line);
						line = [];
					});
				});
			}
			
			/*			 
			 * Adds feed			
			 */
			$("#button_rss").click(function() {
				$.ajax({
					type : "GET",
					url : "/rss/ajax/addFeed",
					data : "rss=" + $("#rss").val(),
					success : function(response) {
						$("#rss").val("");						
						if (!$.isEmptyObject(response)) {						
							buildFeed(response);
							buildFeedEdit(response);
						}						
					},
					error : function() {
						$("#rss").val("");
						$("#errorId").addClass("error").text("Invalid rss channel").show();
					}
				});
			});

			
			/*
			 * Builds the string to be displayed as a feed in feeds tab.
			 */
			function buildFeed(data) {
				var id = data[0];
				var title = data[1];				
				var feedId = "feed" + id;
				
				var feed = $("<li></li>");
				feed.attr("id", feedId);
				var div_image = $("<img></img>");				
				div_image.attr("src", "resources/images/rss-image.jpg");							
				div_image.addClass("feed_image");
				var div_title = $("<div></div>");
				div_title.addClass("feed_title");
				div_title.text(title);
				var div_counts = $("<div></div>");
				div_counts.addClass("counts");
				var a = $("<a></a>");				
				a.append(div_image, div_title, div_counts);
				feed.append(a);
				$("#list_feeds").append(feed);				
				
				$("#list_feeds li:last").click(function() {										
					$("#current-feed").text(title);
					onFeedClick(this);
				});
			}

			/*
			 * Builds the string to be displayed as a feed in edit tab.
			 */
			function buildFeedEdit(data) {
				var id = data[0];
				var title = data[1];				
				var feedEditId = "feedEditId" + id;
				
				var feed = $("<li></li>");
				feed.css({"padding" : "5px 5px"});
				feed.attr("id", feedEditId);
				var div_image = $("<img></img>");
				div_image.addClass("feed_image");
				div_image.attr("src", "resources/images/rss-image.jpg");				
				var div_title = $("<div></div>");
				div_title.addClass("feed_title");
				div_title.text(title);
				var parent = $("<div></div>");
				parent.addClass("feed-edit");
				parent.append(div_image, div_title);
				var delButton = $("<button>delete</button>");
				delButton.attr("id", id);
				feed.append(parent, delButton);
				$("#list_feeds_edit").append(feed);				
				
				$("button:last").click(function() {
					//alert("delete buttton: " + delButton.attr("id"));
					deleteFeed(this);
				});
			}

			/*
			 * Forms the items' list
			 */
			function onFeedClick(obj) {				
				var feedId = $(obj).attr("id").match(/\d+/); 				
				//alert("feedId: " + feedId);
				if (!($("#list_items").is(":empty"))) {
						$("#list_items").empty();
				}
				
				$.ajax({
					type : "GET",
					dataType : "json",
					url : "/rss/ajax/convertedItems",
					data : "feedId=" + feedId,
					success : function(response) {
						//alert("response: " + response);
						formItems(response);
					},
					error : function(e) {
						alert("Error onFeedClick: " + e);
					}
				});

			}

			function deleteFeed(button) {				
				var feedId = $(button).attr("id").match(/\d+/);
				//alert("delete feedId: " + feedId);
				$.ajax({
					type : "GET",
					url : "/rss/ajax/deleteFeed",
					data : "feedId=" + feedId,
					success : function() {
						//alert("success!");
						$("#feedEditId"+feedId).remove();
						$("#feed"+feedId).remove();
						$("#list_items").empty();
						$("#current-feed").text("");
					},
					error : function(e) {
						alert("Error delete feed: " + e);
					}
				});
			}
			

			/*
			 * Builds string to be displayed as an item in a items' list.
			 */
			function buildItem(item) {
				var obj = new Object();
				obj.id = item[0];
				obj.title = item[1];
				obj.link = item[2];
				obj.description = item[3];				
				obj.pubDate = item[4];
				obj.readMark = item[5];
				obj.dateMark = item[6];
				obj.starMark = item[7];
				obj.toString = function() {
					return this.id + "; " + this.title + "; " + this.link
							+ "; " + this.description + "; " + this.pubDate;
				};
				//alert("instanse: " + obj.starMark);
				var item = $("<li></li>");

				/*Build flip*/
				var idFlip = "flip" + obj.id;
				var idStar = "star" + obj.id;
				var div_image = $("<img></img>");
				div_image.attr("id", idStar);
				div_image.attr("mark", obj.starMark);
				if (obj.starMark == false) {					
					div_image.attr("src", "resources/images/star-unliked.jpg");	
				} else {
					div_image.attr("src", "resources/images/star-liked.jpg");
				}
				div_image.addClass("item_image");
				var div_title = $("<div></div>");
				div_title.addClass("item_title");
				div_title.text(obj.title);
				var div_pubdate = $("<div>time</div>");
				div_pubdate.addClass("item_pubdate");
				div_pubdate.text(obj.pubDate + "" + obj.dateMark);
				var a_flip = $("<a></a>");
				a_flip.attr("id", idFlip);
				a_flip.append(div_image, div_title, div_pubdate);

				/*Build panel*/
				var idPannel = "pannel" + obj.id;
				var div_pannel = $("<div></div>");				
				div_pannel.attr("id", idPannel);
				div_pannel.addClass("panel");
				var div_title_pannel = $("<div></div>");
				div_title_pannel.addClass("title-panel");
				div_title_pannel.text(obj.title);
				var div_description = $("<div></div>");
				div_description.html(obj.description);
				var a_read = $("<a></a>");
				a_read.attr("href", obj.link);
				a_read.attr("target", "_blank");
				a_read.text("read more...");
				div_pannel.append(div_title_pannel, div_description, a_read);

				item.append(a_flip, div_pannel);
				$("#list_items").append(item);

				$("#list_items li:last").click(function() {
					// alert("Item clicked!");
					$("#" + idPannel).slideToggle();
					$("#" + idFlip).toggle();
				});
				$("#"+idStar).click(function(event) {					
					starClicked(this);
					event.stopPropagation();
				});
			};			
			
			/*
			 * Mark/unmark as starred
			 */
			function starClicked(star) {				
				var id = $(star).attr("id").match(/\d+/);
				//alert("star id: " + id);
				$.ajax({
					type : "GET",
					url : "/rss/ajax/starFlag",
					data : "itemId=" + id,
					success : function() {
						//alert("success!");
						if ($(star).attr("mark") == "false") {
							$(star).attr("mark", "true");
							$(star).attr("src", "resources/images/star-liked.jpg");
						} else {
							$(star).attr("mark", "false");
							$(star).attr("src", "resources/images/star-unliked.jpg");
						}
					},
					error : function(e) {
						alert("Error star: " + e);
					}
				});				
			}
			
			/*
			 * Gets starred items
			 */
			$("#starred-items").click(function() {
				
				if (!($("#list_items").is(":empty"))) {
					$("#list_items").empty();
				}
				
				$.ajax({
					type : "GET",
					dataType : "json",
					url : "/rss/ajax/staredItems",
					data : "username=" + login,
					success : function(response) {
						formItems(response);
					},
					error : function(e) {
						alert("Error onStarredItemsClick: " + e);
					}
				});	
			});
			
			/*
			 * Gets all items
			 */
			$("#all-items").click(function() {
				
				if (!($("#list_items").is(":empty"))) {
					$("#list_items").empty();
				}
				
				$.ajax({
					type : "GET",
					dataType : "json",
					url : "/rss/ajax/userItems",
					data : "username=" + login,
					success : function(response) {
						formItems(response);
					},
					error : function(e) {
						alert("Error onStarredItemsClick: " + e);
					}
				});	
			});
			
			function formItems(response) {
				var line = [];
				$.each(response, function(key, value) {
					//alert("key: " + key + ":" + " value: " + value);
					$.each(value, function(k, v) {
						//alert("k: " + k + ":" + " v: " + v);
						line.push(v);
					});
					buildItem(line);
					line = [];
				});
			}
			
		});
